package com.da.javatest.design.schema.singleton;

public class SingletonTest02 {
    //1. 构造器私有化, 外部能new
    private SingletonTest02() {}

    //2.本类内部创建对象实例
    private final static SingletonTest02 instance;

    // 在静态代码块中，创建单例对象
    static {
        instance = new SingletonTest02();
    }

    //3. 提供一个公有的静态方法，返回实例对象
    public static SingletonTest02 getInstance() {
        return instance;
    }

    public static void main(String[] args) {
        //测试
        SingletonTest02 instance = SingletonTest02.getInstance();
        SingletonTest02 instance2 = SingletonTest02.getInstance();
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}
